package com.macro.mall.redis;

import com.macro.mall.dao.SmsFlashPromotionSkuRelationDao;
import com.macro.mall.model.SmsFlashPromotionSkuRelation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author：贾维斯
 * @Description: 描述
 * @name：GoodsRedis
 * @Date：2024/11/1 22:15
 */
@RestController
@EnableScheduling
public class GoodsRedis {
    @Autowired
    private SmsFlashPromotionSkuRelationDao smsFlashPromotionSkuRelationDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 12 * *")
    private void testRedis() {
        //查询所有的sku什么表的信息
       List<SmsFlashPromotionSkuRelation> list= smsFlashPromotionSkuRelationDao.selectAll();
       //判断是否为空
       if (list!=null) {
           for (SmsFlashPromotionSkuRelation smsFlashPromotionSkuRelation : list) {
               //从这个表里面拿到三个那啥Id来作为存入redis的键
             String key =  getRedisKey(smsFlashPromotionSkuRelation.getFlashPromotionId(),smsFlashPromotionSkuRelation.getFlashPromotionSessionId(),smsFlashPromotionSkuRelation.getSkuId());
             //将商品基本信息塞进redis中
             redisTemplate.opsForValue().set(key,list.toString());
           }
       }
    }

    private String getRedisKey(Long flashPromotionId, Long flashPromotionSessionId, Long skuId) {
        //new一个什么buff，这个安全
        StringBuffer buffer = new StringBuffer();
        buffer.append(flashPromotionId);
        buffer.append(":");
        buffer.append(flashPromotionSessionId);
        buffer.append(":");
        buffer.append(skuId);
        return buffer.toString();
    }
}
